<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/4/23
 * Time: 10:20
 */

namespace app\common\model;


use XznsTools\cache\CacheManage;

class ZlAstrInfoCheck extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_astr_info_check';

    protected $cacheModel = 'astr_info_check';

    /**
     *  保存用户认证信息
     * @param array $data 保存或编辑的数据
     * @param array $id 编辑的id
     * @return bool|false|int
     * @throws \Exception
     */
    public function saveAuth($uid = [], $data = [])
    {
        $info = $this->UserAstrInfo($uid, 'id');

        if ($info)
        {
            $this->error = '当前用户已有认证，不可重复认证';
            return false;
        }
        try
        {
            $this->insertGetId($data);
        }
        catch (\Exception $e)
        {
            $this->error = '认证信息保存失败';
            return false;
        }
        // 更新缓存
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)
            ->setCache($data, 'details', false);

        return true;
    }

    /**
     * 更新用户认证信息
     * @param $uid
     * @param $data
     * @return bool|false|int
     * @throws \Exception
     */
    public function updateAuth($uid, $data)
    {
        $info = $this->UserAstrInfo($uid, 'id');

        if (!$info)
        {
            $this->error = '当前用户无认证信息可以编辑，请前去认证';
            return false;
        }
        try
        {
            $re = $this->save($data, ['id' => $info['id']]);
        }
        catch (\Exception $e)
        {
            $this->error = '认证信息编辑失败';
            return false;
        }
        // 更新缓存
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)
            ->setCache($data, 'details', true);

        return $re;
    }


    /**
     * 获取身份证与花名认证信息,用于判断是否重复
     * @param int $uid 用户id
     * @param int $IdNumber 身份证号
     * @param string $NickName 花名
     * @param string $TelPhone 手机号
     * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
     */
    public function getAuthCheck($uid = 0, $IdNumber = 0, $NickName = '', $TelPhone = '')
    {
        //获取审核信息是否重复
        if ($IdNumber && $NickName && $uid && $TelPhone)
        {
            $where = "id_number = $IdNumber OR nickname = $NickName OR uid = $uid OR telphone = $TelPhone";
        } else
        {
            $this->error = '请查看信息，是否有未填写的';
            return false;
        }
        try
        {
            $id = $this->where($where)->column('id', 'id');
            $id = array_flip_key_value($id);
        }
        catch (\Exception $e)
        {
            $id = [];
        }
        $data = $this->getInfoByIdsInData($id, 0, 'id');
        return $data;
    }

    /**
     * 获取用户认证信息
     * @param $uid
     * @param $field
     * @return array|bool|string
     * @throws \Exception
     */
    public function UserAstrInfo($uid, $field)
    {
        $field = is_string($field) ? $field : implode(',', $field);
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)
            ->getCache($field, 'details');
        if ($data === false)
        {
            $where = ['uid' => $uid];
            $field = 'status,id,uid,name,nickname,telphone,id_number,area_ids,left_pic,right_pic,certificate_pic,create_time,update_time';
            try
            {
                $data = $this->where($where)->column($field, 'uid');
                CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)
                    ->setCache($data, 'details', false);
            }
            catch (\Exception $e)
            {
                $data = [];
            }
        }
        return $data;
    }

    /**
     * 数据库批量获取用户认证信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['uid' => ['in', $ids]];
        try
        {
            $data = $this->where($where)->column('status,id,uid,name,nickname,telphone,id_number,area_ids,left_pic,right_pic,certificate_pic,create_time,update_time', 'uid');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}